import { call, put } from 'redux-saga/effects';
import { Axios } from '../../service/axios';
import Citys from '../../config/citys'
import _ from 'lodash'

function fetchCityList(city: string) {
    const citys = {
        '河南': ['郑州', '信阳', '周口', '商丘', '许昌', '驻马店', '洛阳'],
        '北京': ['北京'],
        '上海': ['黄浦区', '杨浦区', '浦东新区', '徐汇区', '闵行区', '虹口区', '静安区'],
    }
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (citys[city]) {
                resolve({
                    flag: 1,
                    result: citys[city]
                })

            } else {
                reject({
                    flag: 0,
                })
            }
        }, 1000)
    })
}

export function* getCityList(action?: any) {
    const params = action.params || {};
    try {
        const response = yield call(fetchCityList, params);
        yield put({
            type: 'CityList',
            data: response
        })
    } catch (error) {

    }
}

export function* getWeatherInfo(action?: any) {
    const params = action.params || {};
    try {
        const response = yield call(Axios.get, params);
        yield put({
            type: 'WeatherInfo',
            data: response.data
        })
    } catch (error) {

    }
}

export function* getGeographicalLocation(action?: any) {
    const params = action.params || {};
    try {
        const response = yield call(Axios.get, params);
        let city = response.data.city;
        const cityInfo = _.filter(Citys, item => {
            return city.indexOf(item.ReadmeName) > -1;
        })
        yield put({
            type: 'GeographicalLocation',
            data: cityInfo[0] || {}
        })
    } catch (error) {

    }
}